﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Data;
using UniMana.Utils.Controls;
using UniMana.Utils.Data;
using UniMana.Utils.Helper;
namespace UniMana.Win.Training
{
    public partial class FrmSchedule_SubChooseClass : DevExpress.XtraEditors.XtraForm
    {
        #region Khai báo biến toàn cục
        private bds m_bds = new bds();
        private DBMain m_DBMain = new DBMain();
        //private int m_iTraining_ClassKey;
        private DataTable m_dtaLeft;
        //private DataTable m_dtaRight;
        //private int m_iTotalStudent = -1;
        private int m_iTrainingProgramKey = -1;
        //private int m_iClassKey = -1;
        private int m_iCatalogue_CouserKey = -1;
        private List<int?> m_ilSubjectKey = new List<int?>();
        private int m_iDepartmentKey = -1;
        public DataTable m_dtaClass = new DataTable();

        public List<DataTable> m_l_dta_Student = new List<DataTable>();

        private DataTable m_dtaChoose = new DataTable();

        #region Khởi Tạo Biến
        public List<DataTable> ListStudent
        {
            get { return m_l_dta_Student; }
        }
        //public int TrainingClassKey
        //{
        //    get { return this.m_iTraining_ClassKey; }
        //    set { grvTraining_Class.SetFocusedRowCellValue("TrainingClassKey", value); }
        //}
        //public int TotalStudent
        //{
        //    get { return m_iTotalStudent; }
        //}
        public DataTable Class_Choose
        {
            get { return m_dtaChoose; }
            set { gctTraining_Class_Choose.DataSource = m_dtaChoose; }
        }
        public int TrainingProgramKey
        {
            get { return m_iTrainingProgramKey; }
        }
        public int CouserKey
        {
            get { return Cache.m_iCatalogue_CouserKey; }
            set { lueCatalogue_Course.EditValue = value; }
        }
        public int DepartmentKey
        {
            get { return Cache.m_iCatalogue_DepartmentKey; }
            set { lueCatalogue_Department.EditValue = value; }
        }
        #endregion
        #endregion

        #region Hàm Khởi Tạo
        public FrmSchedule_SubChooseClass()
        {
            InitializeComponent();
            //ShowInTaskbar = false;
            LoadData();
            m_iCatalogue_CouserKey = Cache.m_iCatalogue_CouserKey;
            m_iDepartmentKey = Cache.m_iCatalogue_DepartmentKey;

            m_ilSubjectKey.Clear();
            for (int i = 0; i < Cache.m_ilSubjectKey.Count; i++)
            {
                m_ilSubjectKey.Add(Cache.m_ilSubjectKey[i]);
            }

            lueCatalogue_Course.EditValue = m_iCatalogue_CouserKey;
            lueCatalogue_Department.EditValue = m_iDepartmentKey;

            GetClass();
            grvTraining_Class.ExpandAllGroups();

            ///chưa load dữ liệu cũ (nhớ truyền dữ liệu vào khi làm)
            SetDataClassChoose();
            gctTraining_Class_Choose.DataSource = m_dtaChoose;
            grvTraining_Class.ExpandAllGroups();
            grvTraining_Class_Choose.ExpandAllGroups();
        }
        private void LoadData()
        {
            lueCatalogue_Course.GetData(
                m_DBMain.m_DCReadOnly.Catalogue_Courses,
                "Catalogue_CourseKey",
                "Name",
                chooseAll: true,
                noChoose: true);
            lueCatalogue_Department.GetData(
                m_DBMain.m_DCReadOnly.Catalogue_Departments.GetDepartmentStudentManage(),
                "Catalogue_DepartmentKey",
                "Name",
                noChoose: true);
            //m_dtaRight = new DataTable();
            //m_dtaRight.Columns.Add("Training_StudentKey", typeof(Guid));
            //m_dtaRight.Columns.Add("ID", typeof(string));
            //m_dtaRight.Columns.Add("LastName", typeof(string));
            //m_dtaRight.Columns.Add("SureName", typeof(string));
            //m_dtaRight.Columns.Add("Birthday", typeof(string));
            //m_dtaRight.Columns.Add("Training_TrainingProgramKey", typeof(int));
            //gctTraining_Teacher_Student.DataSource = m_dtaRight;
            m_dtaLeft = new DataTable();

            rluCatalogue_Course.GetData(m_DBMain.m_DCReadOnly.Catalogue_Courses);
            rluCatalogue_Department.GetData(m_DBMain.m_DCReadOnly.Catalogue_Departments);
            rluCatalogue_Level.GetData(m_DBMain.m_DCReadOnly.Catalogue_Levels);
            rluCatalogue_Major.GetData(m_DBMain.m_DCReadOnly.Catalogue_Majors);
            rluCatalogue_TrainingType.GetData(m_DBMain.m_DCReadOnly.Catalogue_TrainingTypes);

            gclDepartment_Choose.ColumnEdit = rluCatalogue_Department;
            gclLevel_Choose.ColumnEdit = rluCatalogue_Level;
            gclCourse_Choose.ColumnEdit = rluCatalogue_Course;
            gclMajor_Choose.ColumnEdit = rluCatalogue_Major;
            gclStatus_Choose.ColumnEdit = cke_Status;
            gclTrainingType_Choose.ColumnEdit = rluCatalogue_TrainingType;
        }
        #endregion

        #region [Các sự kiện trong Form]
        #region Sự Kiện btn
        //
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }        
        //Đồng ý những chọn lựa và đóng Form
        private void btnAgree_Click(object sender, EventArgs e)
        {
            int listclasscount = 0;
            listclasscount = m_dtaChoose.Rows.Count;
            if (listclasscount > 0)
            {
                for (int i = 0; i < listclasscount; i++)
                {
                    if (m_l_dta_Student[i].Rows.Count <= 0)
                    {
                        DialogResult result = new DialogResult();
                        result = MessageBox.Show(
                            "Chưa chọn SV, Bạn có muốn chọn cả lớp",
                            "Thông Báo",
                        MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            SelectSt(i);                            
                        }
                        else
                        {
                            int handle =Convert.ToInt16( m_dtaChoose.Rows[i]["Count"].ToString());
                            grvTraining_Class_Choose.CancelSelection();
                            grvTraining_Class_Choose.SelectRow(handle);
                            btnRemoveCL_Click(sender, e);
                        }
                    }
                }
                //    grvTraining_Student_Class.SelectAll();
                //    SelectSt();

                //m_iTraining_ClassKey =
                //    Convert.ToInt16(
                //    grvTraining_Class.GetFocusedRowCellValue(
                //    "Training_ClassKey").ToString());

                //m_iCatalogue_CouserKey = lueCatalogue_Course.GetIntValue();

                //m_iTotalStudent = m_dtaRight.Rows.Count;
                SaveClassToCache();
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
            else
            {
                XtraMessageBox.Show("Chưa chọn lớp.");
            }
        }
        //
        private void btnView_Click(object sender, EventArgs e)
        {
            if (grvTraining_Class.FocusedRowHandle >= 0)
            {
                m_bds = new bds();
                m_dtaLeft = m_DBMain.m_DCReadOnly.Training_Classes.GetListStudentByClassKey(
                    m_DBMain.m_DCReadOnly.Training_Student_Classes,
                    m_DBMain.m_DCReadOnly.Training_Students,
                    Convert.ToInt16(
                    grvTraining_Class.GetFocusedRowCellValue(
                    "Training_ClassKey").ToString())
                    ).ToDataTable();
                gctTraining_Student_Class.DataSource = m_dtaLeft;
                int m_iClassKey =
                    Convert.ToInt16(
                    grvTraining_Class.GetFocusedRowCellValue(
                    "Training_ClassKey").ToString());
                Training_ClassInfo classInfo = new Training_ClassInfo(m_iClassKey);
                m_iTrainingProgramKey = classInfo.Properties.Training_TrainingProgramKey.Value;
            }
        }
        //Dưa SV vào lớp
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int k = Convert.ToInt16(grvTraining_Class_Choose.GetFocusedRowCellValue("i").ToString());
            SelectSt(k);
        }
        //Xóa sv ra khỏi lớp
        private void btnRemove_Click(object sender, EventArgs e)
        {
            int k = Convert.ToInt16(grvTraining_Class_Choose.GetFocusedRowCellValue("i").ToString());
            RemoveSt(k);

            int count = grvTraining_Class_Choose.SelectedRowsCount;
            if (count > 0)
            {
                if (grvTraining_Class_Choose.FocusedRowHandle >= 0)
                {
                    Setm_dtaLeft();
                }
            }
        }
        //Chọn lớp học
        private void btnAddCL_Click(object sender, EventArgs e)
        {
            int count = grvTraining_Class.SelectedRowsCount;
            if (count > 0)
            {
                foreach (int i in grvTraining_Class.GetSelectedRows())
                {
                    if (i >= 0)
                    {
                        string key = grvTraining_Class.GetRowCellValue(i, "Training_ClassKey").ToString();
                        if (!CheckClass(key))
                        {
                            DialogResult result = new DialogResult();
                            result = MessageBox.Show(
                                "Đã Chọn Lớp " + 
                                grvTraining_Class.GetRowCellValue(i, "ID").ToString() + 
                                " Rồi Bạn Có Muốn Chọn Tiếp Không?",
                                "Thông Báo",
                                MessageBoxButtons.YesNo);

                            if (result == DialogResult.Yes)
                            {
                                ChooseClass(i);
                            }
                        }
                        else
                        {
                            ChooseClass(i);
                        }
                    }
                }
                grvTraining_Class.ExpandAllGroups();
                grvTraining_Class_Choose.ExpandAllGroups();
            }
            else XtraMessageBox.Show("Bạn Chưa Chọn Lớp", "Lỗi");
        }
        //Xóa lớp học
        private void btnRemoveCL_Click(object sender, EventArgs e)
        {
            int count = grvTraining_Class_Choose.SelectedRowsCount;
            if (count > 0)
            {
                DataTable draf = new DataTable();
                CoppyData_Add(m_dtaChoose, ref draf);
                int countremove = 0;
                foreach (int i in grvTraining_Class_Choose.GetSelectedRows())
                {
                    if (i >= 0)
                    {
                        DialogResult result = new DialogResult();
                        result = MessageBox.Show(
                            "Bạn Có Chắc Bỏ Chọn Lớp" +
                            grvTraining_Class_Choose.GetRowCellValue(i, "ID").ToString() +
                            " Không?",
                            "Thông Báo",
                            MessageBoxButtons.YesNo);

                        if (result == DialogResult.Yes)
                        {

                            int k = Convert.ToInt16(
                                grvTraining_Class_Choose.GetRowCellValue(i, "i").ToString());
                            RemoveClass(k - countremove, draf);
                            countremove++;
                            m_l_dta_Student.RemoveAt(k);
                        }
                    }
                }
                CoppyData_Add(draf, ref m_dtaChoose);
                ResetIndexm_dtaChoose(m_dtaChoose);
                grvTraining_Class.ExpandAllGroups();
                grvTraining_Class_Choose.ExpandAllGroups();
            }
            else XtraMessageBox.Show("Bạn Chưa Chọn Lớp", "Lỗi");
        }      
        #endregion

        #region  Sự Kiện lue
        //Chọn khoa khác(Load lại danh sách lớp)
        private void lueCatalogue_Department_EditValueChanged(object sender,EventArgs e)
        {
            GetClass();
        }
        //Chọn Khóa khác(Load lại danh sách lớp)
        private void lueCatalogue_Course_EditValueChanged(object sender,EventArgs e)
        {
            GetClass();
        }
        #endregion       

        #region Sự Kiện grv
        private void grvTraining_Class_FocusedRowChanged(
            object sender,
            DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            grvTraining_Class.ExpandAllGroups();
            grvTraining_Class_Choose.ExpandAllGroups();
        }
        //load danh sách sinh viên của lớp khi chọn lớp
        private void grvTraining_Class_Choose_FocusedRowChanged(
            object sender,
            DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            
        }
        private void grvTraining_Class_Choose_RowClick(
            object sender, 
            DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            int count = grvTraining_Class_Choose.SelectedRowsCount;
            if (count > 0)
            {
                if (grvTraining_Class_Choose.FocusedRowHandle >= 0)
                {
                    Setm_dtaLeft();
                    gctTraining_Student_Class.DataSource = m_dtaLeft;
                    grvTraining_Student_Class.ExpandAllGroups();
                    grvTraining_Teacher_Student.ExpandAllGroups();
                }
            }
        }
        #endregion
        #endregion 
    }
}

///Save
/*
private void grvTraining_Class_Choose_FocusedRowChanged(
            object sender,
            DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            int count = grvTraining_Class_Choose.SelectedRowsCount;
            if (count > 0)
            {
                if (grvTraining_Class_Choose.FocusedRowHandle >= 0)
                {
                    m_bds = new bds();
                    m_dtaLeft = m_DBMain.m_DCReadOnly.Training_Classes.GetListStudentByClassKey(
                        m_DBMain.m_DCReadOnly.Training_Student_Classes,
                        m_DBMain.m_DCReadOnly.Training_Students,
                        Convert.ToInt16(
                            grvTraining_Class_Choose.GetFocusedRowCellValue(
                                "Training_ClassKey").ToString())
                        ).ToDataTable();

                    for (int i = 0; i < m_dtaLeft.Rows.Count; i++)
                    {
                        for (int j = 0; j < m_dtaRight.Rows.Count; j++)
                        {
                            if (m_dtaLeft.Rows[i]["Training_StudentKey"] ==
                                m_dtaRight.Rows[j]["Training_StudentKey"])
                            {
                                m_dtaLeft.Rows.RemoveAt(i);
                                i--;
                                break;
                            }
                        }
                    }

                    gctTraining_Student_Class.DataSource = m_dtaLeft;
                    m_iClassKey = Convert.ToInt16(
                        grvTraining_Class_Choose.GetFocusedRowCellValue(
                        "Training_ClassKey").ToString());
                    Training_ClassInfo classInfo = new Training_ClassInfo(m_iClassKey);
                    m_iTrainingProgramKey = classInfo.Properties.Training_TrainingProgramKey.Value;

                    grvTraining_Class.ExpandAllGroups();
                    grvTraining_Class_Choose.ExpandAllGroups();
                }
            }
        }
 * 
        private void btnRemove_Click(object sender, EventArgs e)
        {
            int[] row = grvTraining_Teacher_Student.GetSelectedRows();
            if (row.Length > 0)
            {
                for (int i = 0; i < row.Length; i++)
                {
                    Guid personelKey =
                        grvTraining_Teacher_Student.GetRowCellValue(
                            row[i],
                            gclTraining_StudentKey_Right).ToGuid();
                    string strID =
                        grvTraining_Teacher_Student.GetRowCellValue(
                            row[i],
                            gclID_Right).ToString();
                    string strLastName =
                        grvTraining_Teacher_Student.GetRowCellValue(
                            row[i],
                            gclLastName_Right).ToString();
                    string strSureName =
                        grvTraining_Teacher_Student.GetRowCellValue(
                            row[i],
                            gclSureName_Right).ToString();
                    string strBirthday =
                        grvTraining_Teacher_Student.GetRowCellValue(
                            row[i],
                            gclBirthday_Right).ToString();
                    int iTraining_TrainingProgramKey =
                        grvTraining_Teacher_Student.GetRowCellValue(
                            row[i],
                            gclTraining_TrainingProgramKey_Right).ToInt();
                    m_dtaLeft.Rows.Add(
                        personelKey,
                        strID,
                        strLastName,
                        strSureName,
                        strBirthday,
                        iTraining_TrainingProgramKey);
                }
                grvTraining_Teacher_Student.DeleteSelectedRows();
            }
        }
*/